<template>
  <div class="basic-info">
    <div class="basic-body common-content"
         v-loading="!show"
         element-loading-text="拼命加载中"
         element-loading-background="rgba(0, 0, 0, 0)">
      <el-scrollbar style="height: 100%;">
        <div class="scrollbar-box">
          <div class="comp-item-group" v-if="show">
            <div class="comp-item-box">
              <div class="comp-item-title">应用基本信息</div>
              <div class="comp-item-cont">
                <el-form
                    ref="basicForm"
                    size="small"
                    label-width="100px">
                  <el-row :gutter="15">
                    <el-col :span="8">
                      <el-form-item label="应用名称" prop="appName">
                        <el-input v-model="basicForm.appName" readonly></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="应用版本号" prop="appVersion">
                        <el-input v-model="basicForm.appVersion" readonly></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="应用类型" prop="appType">
                        <el-input v-if="basicForm.appType===1" value="单组件应用" readonly></el-input>
                        <el-input v-if="basicForm.appType===2" value="组合应用" readonly></el-input>
                        <el-input v-if="basicForm.appType===3" value="单执行应用" readonly></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="数据入库" prop="appIndb">
                        <el-input v-if="basicForm.appIndb===0" value="入库" readonly></el-input>
                        <el-input v-if="basicForm.appIndb===1" value="不入库" readonly></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8" v-if="basicForm.appIndb==='0'">
                      <el-form-item label="数据库名称" prop="appVersion">
                        <el-input v-model="basicForm.appDBName" readonly></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="所属资源" prop="resourceId">
                        <el-select
                            class="w100"
                            v-model="basicForm.resourceId"
                            placeholder="选择所属资源" readonly disabled>
                          <el-option
                              v-for="(item, index) in resource"
                              :key="index"
                              :label="item.resName"
                              :value="item.id"></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="是否监听" prop="isMonitor">
                        <el-input v-if="basicForm.isMonitor==='0'" value="监听" readonly></el-input>
                        <el-input v-if="basicForm.isMonitor==='1'" value="不监听" readonly></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="是否切换" prop="isSwitch">
                        <el-input v-if="basicForm.isSwitch==='0'" value="切换" readonly></el-input>
                        <el-input v-if="basicForm.isSwitch==='1'" value="不切换" readonly></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="24">
                      <el-form-item label="版本说明" prop="appRemarks">
                        <el-input type="input" v-model="basicForm.appRemarks" readonly></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="24">
                      <el-form-item label="场景描述" prop="appScene">
                        <el-input type="textarea" v-model="basicForm.appScene" readonly></el-input>
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-form>
              </div>
            </div>

            <div class="comp-item-box">
              <div class="comp-item-title">填写执行计划</div>
              <div class="comp-item-cont">
                <el-form
                    :model="planForm"
                    :rules="planRules"
                    ref="planForm"
                    size="small"
                    label-width="100px">
                  <el-row :gutter="15">
                    <el-col :span="8">
                      <el-form-item label="计划名称" prop="planName">
                        <el-input v-model="planForm.planName" placeholder="请填写计划名称"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="计划类型" prop="executeType">
                        <el-select
                            class="w100"
                            v-model="planForm.executeType"
                            placeholder="请选择计划类型">
                          <el-option label="即时执行" :value="0"></el-option>
                          <el-option label="常驻进程" :value="1"></el-option>
                          <el-option label="周期执行" :value="2"></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8" v-if="this.planForm.executeType == 2">
                      <el-form-item label="执行单元" prop="executeUnit">
                        <el-select
                            class="w100"
                            v-model="planForm.executeUnit"
                            placeholder="请选择执行单元">
                          <el-option label="每天" value="0"></el-option>
                          <el-option label="每周" value="1"></el-option>
                          <el-option label="每月" value="2"></el-option>
                          <el-option label="每年" value="3"></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8" v-if="this.planForm.executeType == 2">
                      <el-form-item label="时间" prop="executeTime">
                        <el-input v-model="planForm.executeTime" placeholder="请填写计划时间"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8" v-if="this.planForm.executeType == 2">
                      <el-form-item label="最小周期" prop="executeDay">
                        <el-input v-model="planForm.executeDay" placeholder="请填写计划最小周期"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="备注" prop="remarks">
                        <el-input v-model="planForm.remarks" placeholder="请填写计划备注"></el-input>
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-form>
              </div>
            </div>

            <div class="comp-item-box">
              <div class="comp-item-title">录入运行环境</div>
              <div class="comp-item-cont">
                <el-form
                    :model="environmentForm"
                    :rules="environmentRules"
                    ref="environmentForm"
                    size="small"
                    label-width="100px">
                  <el-row :gutter="15">
                    <el-col :span="8">
                      <el-form-item label="使用系统" prop="systemId">
                        <el-select
                            class="w100"
                            v-model="environmentForm.systemId"
                            placeholder="请选择使用系统">
                          <el-option
                              v-for="item in systemOptions"
                              :key="item.id"
                              :label="item.systemName"
                              :value="item.id">
                          </el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="最小CPU">
                        <el-input
                            type="number"
                            v-model="environmentForm.requireCPU"
                            placeholder="请填写运行最小CPU"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="最小存储单元">
                        <el-input
                            type="number"
                            v-model="environmentForm.requireDisk"
                            placeholder="请填写运行最小存储单元"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="最小GPU">
                        <el-input
                            type="number"
                            v-model="environmentForm.requireGPU"
                            placeholder="请填写运行最小GPU"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="运行内存要求">
                        <el-input
                            type="number"
                            v-model="environmentForm.requireMemory"
                            placeholder="请填写运行内存要求"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item class="up" label="上传环境">
                        <el-input v-model="environmentForm.requirePath" placeholder="请上传运行环境">
                          <el-upload
                              slot="append"
                              class="upload-demo"
                              action="#"
                              :show-file-list="false"
                              :http-request="environmentFile">
                            <el-button size="small" type="primary" :loading="fileUpLoading">点击上传</el-button>
                          </el-upload>
                        </el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="备注" prop="remarks">
                        <el-input
                            type="input"
                            v-model="environmentForm.remarks" placeholder="请填写运行环境备注"></el-input>
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-form>
              </div>
            </div>

            <div class="comp-item-box" v-if="basicForm.isMonitor=='0'">
              <div class="comp-item-title">配置应用监控</div>
              <div class="comp-item-cont">
                <el-form
                    :model="monitorForm"
                    :rules="monitorRules"
                    ref="environmentForm"
                    size="small"
                    label-width="100px">
                  <el-row :gutter="15">
                    <el-col :span="8">
                      <el-form-item label="监控频率/s">
                        <el-input v-model="monitorForm.monitorRate" placeholder="请填写应用监控频率"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="监控类型" prop="monitorType">
                        <el-select
                            class="w100"
                            v-model="monitorForm.monitorType"
                            placeholder="请选择监控类型"
                            @change="changeMonitorType">
                          <el-option label="全部" :value="2"></el-option>
                          <el-option label="进程监控" :value="0"></el-option>
                          <el-option label="数据监控" :value="1"></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="24" v-if="monitorForm.monitorType!==''">
                      <el-form-item label="监控列表">
                        <div style="line-height: 1">
                          <MonitorList
                              ref="monitorList"
                              :data="paroles"></MonitorList>
                        </div>
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-form>
              </div>
            </div>

            <div class="comp-item-box" v-if="basicForm.isSwitch=='0'">
              <div class="comp-item-title">应用故障切换</div>
              <div class="comp-item-cont">
                <el-form
                    :model="switchForm"
                    :rules="switchRules"
                    ref="environmentForm"
                    size="small"
                    label-width="100px">
                  <el-row :gutter="15">
                    <el-col :span="8">
                      <el-form-item label="应用切换名称">
                        <el-input v-model="switchForm.switchName" placeholder="请填写应用切换名称"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="应用切换地址">
                        <el-input v-model="switchForm.switchAddress" placeholder="请填写应用切换地址"></el-input>
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-form>
              </div>
            </div>

            <div class="comp-item-btn-group">
              <el-button type="primary" size="small" @click="btnAll" :loading="btnLoading">保存附属信息</el-button>
            </div>
          </div>
        </div>
      </el-scrollbar>
    </div>
  </div>
</template>

<script>
import {
  get_getResource,
  put_aApplicationInfoAdd,
  get_getAppliesInfo
} from "@/apis/appLC/applc";
import {
  get_getSystem,
} from "@/apis/assembly/assembly";
import MonitorList from './comp/MonitorList'

export default {
  components: {MonitorList},
  data () {
    return {
      show: false,
      // 基本信息表单
      basicForm: {
        appName: "",
        appVersion: "",
        appIndb: "",
        appDBName: "",
        isMonitor: "",
        isSwitch: "",
        appRemarks: "",
        remarks: "",
        resourceId: "",
        appScene: "",
        appType: "",
        id: ""
      },
      // 执行计划
      planForm: {
        planName: "",
        executeType: "",
        executeUnit: "",
        executeTime: "",
        executeDay: "",
        remarks: "",
        id: "",
      },
      planRules: {},
      monitorForm: {
        //配置监控表单
        monitorRate: "",
        monitorType: "",
        pId: "",
        id: "",
      },
      monitorRules: {}, //监控表单校验
      environmentForm: {
        pId: "",
        systemId: "",
        requireCPU: "",
        requireDisk: "",
        requireGPU: "",
        requireMemory: "",
        remarks: "",
        id: "",
        requirePath: "",
        file: "",
        //录入环境表单
      },
      environmentRules: {},
      //--------------------------------------
      switchForm: {
        //组件切换表单
        switchAddress: "",
        switchName: "",
        pId: "",
        id: "",
      },
      switchRules: {},
      resource: [], // 资源外键
      appId: "",
      isAddApp: false,
      systemOptions: [],
      Finfo: "",
      paroles: [], // 监控规则
      fileUpLoading: false,
      btnLoading: false,
    };
  },
  created () {
    this.getResource();
    this.getSystem();
    this.appId = this.$route.query.appid;
    this.getDetails()
  },
  mounted () {
  },
  methods: {
    getDetails () {
      get_getAppliesInfo({appId: this.$route.query.appid}).then((res) => {
        let {applies, aplan, pamonitor, parequire, paswitch} = res.data.data;
        let {data} = res.data;
        if (paswitch) {
          for (const key in this.switchForm) {
            this.switchForm[key] = paswitch[key];
          }
          this.switchForm.pId = paswitch.paId;
        }
        if (applies) {
          for (const key in this.basicForm) {
            this.basicForm[key] = applies[key];
          }
        }

        if (aplan) {
          for (const key in this.planForm) {
            this.planForm[key] = aplan[key];
          }
        }
        if (pamonitor) {
          for (const key in this.monitorForm) {
            this.monitorForm[key] = pamonitor[key];
          }
          this.monitorForm.pId = pamonitor.paId;
        }

        if (parequire) {
          for (const key in this.environmentForm) {
            this.environmentForm[key] = parequire[key];
          }
          this.environmentForm.pId = parequire.paId;
          this.environmentForm.file = parequire.requirePath;
        }

        this.Finfo = data;

        this.show = true
      });
    },
    // 系统下拉
    getSystem () {
      get_getSystem().then((res) => {
        this.systemOptions = res.data.data;
      });
    },
    // 获取资源外键
    getResource () {
      get_getResource().then((res) => {
        this.resource = res.data.data;
      });
    },
    //获取上传文件信息
    success (res, file) {
      this.environmentForm.requirePath = res.data;
    },
    // 上传应用环境文件
    environmentFile (data) {
      this.fileUpLoading = true
      let formData = new FormData()
      formData.append('file', data.file)
      formData.append('id', '')
      this.$api.editPARequireFile(formData).then(res => {
        if (res.data.code === 0) {
          this.environmentForm.requirePath = res.data.data
        }
      }).finally(() => {
        this.fileUpLoading = false
      })
    },
    createParoles (type) {
      if (type == 2) {
        this.paroles = [
          {name: '进程监控', roleType: 0, roleName: '', roleProcessName: '', remarks: ''},
          {name: '数据监控', roleType: 1, roleName: '', roleProcessName: '', remarks: ''}
        ]
      } else if (type == 1) {
        this.paroles = [
          {name: '数据监控', roleType: 1, roleName: '', roleProcessName: '', remarks: ''}
        ]
      } else if (type == 0) {
        this.paroles = [
          {name: '进程监控', roleType: 0, roleName: '', roleProcessName: '', remarks: ''}
        ]
      }
    },
    changeMonitorType (val) {
      this.createParoles(val)
    },
    // 保存后面三张数据
    btnAll () {
      this.planForm.appId = this.appId;
      this.environmentForm.paId = this.appId;
      this.switchForm.paId = this.appId;
      this.monitorForm.paId = this.appId;
      let monitorDatas = this.$refs.monitorList && this.$refs.monitorList.lists

      let opt = {
        aPlan: this.planForm,
        pRequire: this.environmentForm,
        pSwitch: this.basicForm.isSwitch == 0 ? this.switchForm : null,
        pMonitor: this.basicForm.isMonitor == 0 ? this.monitorForm : null,
      };

      if (this.basicForm.isMonitor == 0) {
        opt.roles = monitorDatas ? monitorDatas.map(item => {
          return {
            roleType: item.roleType,
            roleName: item.roleName,
            roleProcessName: item.roleProcessName
          }
        }) : []
      } else {
        opt.roles = null
      }

      this.btnLoading = true
      put_aApplicationInfoAdd({...opt}).then((res) => {
        this.$message.success('保存成功！');
        this.$router.push({
          name: "AppArrange",
          query: {add: 'add', appid: this.appId}
        });
      }).finally(() => {
        this.btnLoading = false
      });
    }
  }
};
</script>
<style lang="less" scoped>

  .scrollbar-box {
    padding: 15px;
  }

  .comp-item-group {
    .comp-item-box {
      margin-bottom: 15px;

      .comp-item-title {
        height: 40px;
        color: #ffffff;
        display: flex;
        align-items: center;
        justify-content: center;
        background-color: rgba(3, 68, 108, 0.8);
        font-size: 17px;
        font-weight: bold;
      }

      .comp-item-cont {
        padding: 15px;
        border: solid 1px rgba(3, 68, 108, 0.5);
      }
    }

    .comp-item-btn-group {
      text-align: center;
      margin-top: 15px;
    }

    /deep/ .el-input-group__append {
      background-color: #66b1ff;
      border-color: #66b1ff;
      color: #ffffff;
    }
  }

  .btnBot {
    width: 100%;
    text-align: center;
    margin-top: 10px;
  }

  .basic-info {
    height: 100%;
    width: 100%;

    .basic-body {
      height: 100%;
      width: 100%;
      overflow: hidden;
    }

    .cont-bottom {
      background: white;
      height: 40px;
      line-height: 40px;
      padding: 0 15px;
    }

    /deep/ .el-collapse-item__header {
      position: relative;
    }

    .cont-main {
      height: 100%;
      padding: 15px;

      .collapseTitle {
        margin-left: 10px;
      }

      .interfaceItems {
        margin-top: 30px;
      }

      .cont_body {
        margin-top: 15px;

        .timeLine {
          width: 1000px;
          margin: 0 auto;

          /deep/ .el-step__title {
            margin-left: -10px;
          }
        }
      }
    }

    .basic {
      display: flex;
      align-items: center;

      .dfc {
        margin-right: 30px;
      }
    }

    .files {
      padding: 0 15px;
      display: flex;
    }

    .addbtn {
      margin-left: 10px;
    }

    .waydiv {
      padding: 0 15px;
    }

    .waylist {
      margin-right: 20px;
    }

    .systm {
      padding: 0 15px;
    }

    .min {
      margin-right: 30px;
    }

    .wid {
      width: 500px;
    }

    .viewbox {
      width: 100%;
      border: 1px solid #dcdcdc;
      position: relative;
      padding: 30px 10px 10px 10px;
      // margin-bottom: 15px;
      .sptitle {
        position: absolute;
        top: -12px;
        background: white;
        left: 10px;
      }

      .jkming {
        margin-right: 50px;
        padding-left: 12px;
        margin-bottom: 15px;
      }
    }
  }

  .btn-class {
    background: #267ff6;
    border: none;
    color: white;
    border-radius: 15px;
    line-height: 30px;
    height: 30px;
    width: 100px;
    position: absolute;
    left: 50%;
    margin-left: -50px;
    cursor: pointer;
  }

  /deep/ .el-form-item--small .el-form-item__content {
    line-height: 33px;
  }
</style>
